/*
 * @Date: 2024-04-01 09:33:35
 * @LastEditors: bingkui.wu
 * @LastEditTime: 2024-04-02 11:27:06
 * @Description: 创建听写记录接口
 */
import { REQUEST } from "#type/request";
import Response from "@common/Response";
import { Paper } from "@models/Paper";
import verifyToken from "@/helper/verifyToken";

const root: REQUEST.BASIC = {
  auth: false,
  method: "post",
  request: async (ctx: any) => {
    try {
      const user = await verifyToken(ctx.header.authorization);

      const { categoryId, gradeId, unitId, unitName, words } = ctx.request.body;

      const { _id } = user as any;
      const createTimes = new Date().getTime();
      words.forEach((word: any) => {
        const { wordId, isCorrect, result } = word;
        Paper.create({
          userId: _id,
          categoryId,
          gradeId,
          unitId,
          unitName,
          wordId,
          createTime: createTimes,
          isCorrect,
          result,
        });
      });

      return Response.Success({});
    } catch (err) {
      console.log(err);
      return err;
    }
  },
};

export default root;
